package com.xxx.rpc.client;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver;

import com.xxx.rpc.client.filters.Filter;
import com.xxx.rpc.client.utils.PropertiesUtils;
import com.xxx.rpc.common.annos.Reference;
import com.xxx.rpc.common.utils.WrapperDubboUtils;
import com.xxx.rpc.common.xml.Dubbo;
import com.xxx.rpc.registry.zookeeper.ZooKeeperServiceDiscovery;


/**
 * @Referance注解使用的自定义的代理工厂，用于将指定的接口转换为代理实体
 */
public class ReferenceBeanFactory implements FactoryBean<Object> ,ApplicationContextAware, EmbeddedValueResolverAware{

	private Class<?> interfaceClass;
    private String  version;
    private final static String registry_address = "rpc.registry_address";
    private final static String register = "rpc.register";
    private static List<Filter> filters=new ArrayList<Filter>();
    private static ZooKeeperServiceDiscovery zooKeeperServiceDiscovery;
	public Class<?> getInterfaceClass() {
		return interfaceClass;
	}

	public void setInterfaceClass(Class<?> interfaceClass) {
		this.interfaceClass = interfaceClass;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public ReferenceBeanFactory(Class<?> interfaceClass, String version) {
		super();
		this.interfaceClass = interfaceClass;
		this.version = version;
	}
	private ApplicationContext applicationContext;
	private StringValueResolver resolver;

	@Override
	public void setEmbeddedValueResolver(StringValueResolver resolver) {
		this.resolver = resolver;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
	public Object createBean() {
		if(zooKeeperServiceDiscovery==null) {
			//说明这个是xml配置下的dubbo
			Dubbo dubbo = WrapperDubboUtils.wrapper(applicationContext, resolver);
			zooKeeperServiceDiscovery=new ZooKeeperServiceDiscovery(dubbo.getRegistry().getAddress());
		}
		RpcProxy rpcProxy = new RpcProxy(zooKeeperServiceDiscovery,filters,Boolean.parseBoolean((String)PropertiesUtils.getCommonYml(register)));
		if(StringUtils.isEmpty(version)) {
			return  rpcProxy.create(interfaceClass);
		}
		return  rpcProxy.create(interfaceClass,version);
	}

	@Override
	public Object getObject() throws Exception {
		return createBean();
	}

	@Override
	public Class<?> getObjectType() {
		return interfaceClass;
	}

	public ReferenceBeanFactory() {
		super();
	}

	@Override
	public boolean isSingleton() {
		return false;
	}
	public static ReferenceBeanFactory getConsumerBean(String beanName, Class<?> field, Reference reference) {
		zooKeeperServiceDiscovery=new ZooKeeperServiceDiscovery((String) PropertiesUtils.getCommonYml(registry_address));
		return new ReferenceBeanFactory(field, reference.version());
	}
}